$(document).ready(function() {
    //data table
    $('#dataSortTable').dataTable({
        "bJQueryUI": true,
        "bPaginate": true,
        "sPaginationType": "full_numbers",
        "bFilter": true,
        "bSort": true,
        "iDisplayLength": 5,
        "aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]]
    });


    //searh function drop downlist
    $("select").change(function () {
        var url = document.institution_form.elements['url1'].value;
        var institutionList = $("#EduInstitution option:selected").val();
        var countryID  = $("#country option:selected").val();
        $("#searchList").load(url +'/'+countryID+ '/'+ institutionList);
    });


    $('#selectInsConfirm').click(function(){
        var amt = $('input[name="institution_id[]"]:checked').length;
        if(amt == 0){
            alert("Please select at least one institution");
        }else{
            var response = confirm('Select all marked ? ('+amt+' institution(s) marked)');
            if(response){
                var allVals = [];
                $('input[name="institution_id[]"]:checked').each(function() {
                    allVals.push($(this).val());
                });
                var url = document.insForm.elements['url4'].value;
                $.post(url,{
                    'institution_id':allVals
                },function(data){
                    alert(data);
                    window.location.reload();
                })
            }
        }
    });

    $('#remove').click(function(){
        var amt = $('input[name="institution_id[]"]:checked').length;
        if(amt == 0){
            alert("Please select at least one institution to remove");
        }else{
            var response = confirm('Remove all marked ? ('+amt+' institution(s) marked)');
            if(response){
                var allVals = [];
                $('input[name="institution_id[]"]:checked').each(function() {
                    allVals.push($(this).val());
                });
                var urls = $("#url5").val();
                $.post(urls,{
                    'institution_id':allVals
                },function(data){
                    alert(data);
                    window.location.reload();
                })

            }
        }
    });
    var elements = ["1", "2", "3"];
    jQuery.each(elements,function(index,element){
        element = this;
        $("#insName"+element).live("click",function() {
            var id = $("#ins_id"+element).val();
            var urls = $("#url_course").val();
            $("#courseList"+element).hide();
            $("#courseList"+element).load(urls +'/'+id);
            $("#courseList"+element).show(); //  use toggle for hiding/show as per current state
        });
    });



    $("#insSubmit").click(function(){
        var country = $("#country option:selected").val();
        var EduInstitution = $("#EduInstitution option:selected").val();
        if( country == "countrynull" &&  EduInstitution == "cat" ){
            alert('Please select your search term!');
            return false;
        }
    });

    checked = false;
    $("#checkAll").click(function () {
        if (checked == false){
            checked = true
        }else{
            checked = false
        }
        for (var i = 0; i < document.getElementById('insForm').elements.length; i++) {
            document.getElementById('insForm').elements[i].checked = checked;
        }
    });


    marked = false;
    $("#markAll").click(function () {
        if (marked == false){
            marked = true
        }else{
            marked = false
        }
        for (var i = 0; i < document.getElementById('selForm').elements.length; i++) {
            document.getElementById('selForm').elements[i].checked = marked;
        }
    });

});







